<?php
class Article_Model extends CI_Model
{
    public $name;
    public $content;
    public $create_time;
    public $update_time;
    public $hits;
    public $status;
    public $author;
    public $source;
    public $type_id;


    function __construct()
    {
        $this->setTableName("articles");
        parent::__construct();
    }


    /**
     * 批量删除
     * @param $ids
     * @return mixed
     */
    public function batchRemove($ids)
    {
        return $this->db->where_in("id", $ids)->update($this->getTableName(), array("status" => 0));
    }

    /**
     * 搜索方法
     */
    public function pagination($id, $start, $limit)
    {
        $this->load->model("Article_Type_Model");
        $this->setQuery($this->db->where_in("type_id", $this->Article_Type_Model->getSqlIds($id))->where("status", 1)->order_by("update_time","desc")->get($this->getTableName(), $start, $limit));
        return $this->mappingList();
    }

    public function paginationCount($typeId)
    {
        $this->load->model("Article_Type_Model");

        $q = $this->db
            ->where_in('type_id', $this->Article_Type_Model->getSqlIds($typeId))
            ->where('status', 1)
            ->get($this->getTableName());

        return $q->num_rows;
    }

    /**
     * 搜索方法
     */
    public function managepagination($start, $limit)
    {
        $this->setQuery($this->db->where("status", 1)->order_by("update_time","desc")->get($this->getTableName(), $start, $limit));
        return $this->mappingList();
    }

    /**
     * 取得某分类下文章的摘要
     * @param $typeId
     * @return mixed取得
     */
    public function findLast($typeId, $count)
    {
        $q = $this->db
            ->where('type_id', $typeId)
            ->where('status', 1)
            ->order_by("update_time","desc")
            ->limit($count)->get($this->getTableName());
        return $q->result();
    }


    public function getManageTotalCount()
    {
        $q = $this->db
            ->where('status', 1)
            ->get($this->getTableName());
        return $q->num_rows;
    }

    public function getTypeName()
    {
        $this->load->model("Article_Type_Model");
        $this->Article_Type_Model->id = $this->type_id;
        return $this->Article_Type_Model->load()->name;
    }


}